package com.dkd.manage.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.dkd.common.core.domain.BaseEntity;
import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.Dto.AddTaskDto;
import com.dkd.manage.domain.Dto.TaskDto;
import com.dkd.manage.domain.Emp;
import com.dkd.manage.domain.TaskDetails;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.mapper.EmpMapper;
import com.dkd.manage.mapper.TaskDetailsMapper;
import com.dkd.manage.mapper.VendingMachineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.TaskMapper;
import com.dkd.manage.domain.Task;
import com.dkd.manage.service.ITaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运营工单Service业务层处理
 *
 * @author zzg
 * @date 2025-03-04
 */
@Service
public class TaskServiceImpl implements ITaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private TaskDetailsMapper taskDetailsMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    /**
     * 查询运营工单
     *
     * @param taskId 运营工单主键
     * @return 运营工单
     */
    @Override
    public Task selectTaskByTaskId(Long taskId) {
        return taskMapper.selectTaskByTaskId(taskId);
    }

    /**
     * 查询运营工单列表
     *
     * @param taskDto 运营工单
     * @return 运营工单
     */
    @Override
    public List<Task> selectTaskList(TaskDto taskDto) {
        return taskMapper.selectTaskList(taskDto);
    }

    /**
     * 新增运营工单
     *
     * @param task 运营工单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTask(AddTaskDto task) {
        //为工单表添加一条数据
        task.setCreateTime(DateUtils.getNowDate());
        String code = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        int count = taskMapper.selectCountByTaskCode(code);
        if(count>=1000){
            task.setTaskCode(code+count);
        }else {
            task.setTaskCode(code+String.format("%04d",count+1));
        }
        task.setTaskStatus(1L);
        Emp emp = empMapper.selectEmpById(task.getUserId());
        task.setUserName(emp.getUserName());
        VendingMachine vm = vendingMachineMapper.selectVendingMachineByInnerCode(task.getInnerCode());
        task.setRegionId(vm.getRegionId());
        task.setAddr(vm.getAddr());
        int result = taskMapper.insertTask(task);
        //为补货详情表增加n条数据
        List<TaskDetails> taskDetailsList = task.getTaskDetailsList();
        if (taskDetailsList != null && taskDetailsList.size() > 0) {
            for (TaskDetails taskDetails : taskDetailsList) {
                taskDetails.setTaskId(task.getTaskId());
            }
            taskDetailsMapper.insertTaskDetailsList(taskDetailsList);
        }
        return result;
    }

    /**
     * 修改运营工单
     *
     * @param task 运营工单
     * @return 结果
     */
    @Override
    public int updateTask(Task task) {
        task.setUpdateTime(DateUtils.getNowDate());
        if(task.getUserId()!=null){
            Emp emp = empMapper.selectEmpById(task.getUserId());
            task.setUserName(emp.getUserName());
        }
        return taskMapper.updateTask(task);
    }

    /**
     * 批量删除运营工单
     *
     * @param taskIds 需要删除的运营工单主键
     * @return 结果
     */
    @Override
    public int deleteTaskByTaskIds(Long[] taskIds) {
        return taskMapper.deleteTaskByTaskIds(taskIds);
    }

    /**
     * 删除运营工单信息
     *
     * @param taskId 运营工单主键
     * @return 结果
     */
    @Override
    public int deleteTaskByTaskId(Long taskId) {
        return taskMapper.deleteTaskByTaskId(taskId);
    }
}
